//Shirasu Azusa 2024.5
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x)  {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x)  {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x)  {return (x==0?-1:__builtin_ctzll(x));}

#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;  
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} 
  return x*w;
}

const int N=1e6+5,mod=998244353;
const ld eps=1e-10;
int T,n,a[N],cnt[N],b[N],m;
ld f[N];

int ksm(int x,int y,int r=1) {
  for(;y;y>>=1,x=x*x%mod) if(y&1) r=r*x%mod;
  return r;
}

void work() {
  n=read(); int a0=read(), A=read(), B=read(), C=read(), M=read();
  a[0]=a0; rep(i,1,n) a[i]=(A*a[i-1]%M*a[i-1]%M+B*a[i-1]%M+C)%M+1;
  rep(i,1,n) cnt[i]=0; m=0;
  rep(i,1,n) cnt[a[i]]++;
  rep(i,1,M) if(cnt[i]) b[++m]=cnt[i];
  ld l=0.333,r=1;
  for(;r-l>eps;) {
    ld mid=(l+r)/2;
    f[1]=(b[1]>1?-mid:0);
    rep(i,2,m) {
      if(b[i]>1) f[i]=f[i-1]+(1-3*mid);
      else f[i]=f[i-1]+(1-2*mid);
      if(b[i]==1) chmin(f[i],0);
      else chmin(f[i],-mid);
    }
    ld res=0;
    rep(i,1,m) chmin(res,f[i]);
    if(res<-1) r=mid;
    else l=mid;
  }
  ld res=1; int x=0,y=0;
  rep(i,2,4*n) {
    int fz=round(i*l); ld rr=1.*fz/i-l;
    if(fabs(rr)<res) res=fabs(rr), x=fz, y=i;
  }
  printf("%.6Lf %lld\n",l,ksm(y,mod-2)*x%mod);
}

signed main() {
  freopen("guess.in","r",stdin);
  freopen("guess.out","w",stdout);
  T=read(); while(T--) work();
  return 0;
}